/*
    Heart of Gold engine management system
    Copyright (C) 2004 Heart of Gold development team

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

/**
 * @author Greg Feigenson
 * @author Ryan Higgins
 * @version RC1
 * @since 0.0 
 * 
 * The Container JFrame. Contains all the JPanels
 * in the rest of the application and the "shell" for
 * the app.
 */

package com.heartOfGold.view;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import com.heartOfGold.controller.Component;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import java.util.Vector;

public class Ui extends JFrame implements ActionListener {
    public Ui(final Component[] components) {
        /* set up panel */
        setTitle("Heart of Gold " + version);
        setSize(800, 600);
        setBackground(Color.gray);

        /* Parent Panel */
        JPanel topPanel = new JPanel(new BorderLayout());
        JPanel northPanel = new JPanel(new BorderLayout());
        JPanel southPanel = new JPanel(new FlowLayout());
        getContentPane().add(topPanel);
        topPanel.add(northPanel, BorderLayout.NORTH);
        topPanel.add(southPanel, BorderLayout.SOUTH);

        /* Create the card container */
        labels = new String[components.length];

        for (int i = 0; i < components.length; ++i) {
            Component toAdd = components[i];
            String label = toAdd.getPanelLabel();
            cardPanel.add(label, toAdd.getJPanel());
            cards.add(label);
            labels[i] = label;
        }

        MainWindow mainWindow = new MainWindow(labels);
        cardPanel.add("Main", mainWindow);
        topPanel.add(cardPanel, BorderLayout.CENTER);

        /* Set up card container */
        l = (CardLayout) cardPanel.getLayout();
        l.show(cardPanel, "Main");

        /* Set up the JButtons */
        JButton quitButton = new JButton("Quit");
        JButton menuButton = new JButton("Main Menu");
        JButton helpButton = new JButton("Help");
        JButton panicButton = new JButton("DON'T PANIC!");
        southPanel.add(quitButton);
        southPanel.add(menuButton);
        southPanel.add(helpButton);
        southPanel.add(panicButton);
        quitButton.addActionListener(this);
        menuButton.addActionListener(this);
        helpButton.addActionListener(this);
        panicButton.addActionListener(this);

        /* menu bar */
        menuBar = new JMenuBar();
        setJMenuBar(menuBar);

        /* menu items - File menu */
        JMenu menuFile = new JMenu("File");
        JMenuItem menuExit = new JMenuItem("Exit", KeyEvent.VK_X);
        menuFile.setMnemonic(KeyEvent.VK_F);
        menuBar.add(menuFile);
        menuFile.add(menuExit);

        /* menu items -- Help menu */
        JMenu menuHelp = new JMenu("Help");
        JMenuItem menuHelpMe = new JMenuItem("Help", KeyEvent.VK_E);
        JMenuItem menuAbout = new JMenuItem("About", KeyEvent.VK_A);
        menuHelp.setMnemonic(KeyEvent.VK_H);
        menuBar.add(menuHelp);
        menuHelp.add(menuHelpMe);
        menuHelp.add(menuAbout);

        /* listeners */
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        menuExit.addActionListener(this);
        menuAbout.addActionListener(this);
        menuHelpMe.addActionListener(this);
    }

    public final void actionPerformed(final ActionEvent e) {
        String command = e.getActionCommand();

        if (command.equals("Exit") || command.equals("Quit")) {
            System.exit(0);
        }
        if (command.equals("About")) {
            JOptionPane.showMessageDialog(this, "Heart of Gold\n"
                    + "Developed by:\n" + "              William Dillon\n"
                    + "              Greg Feigenson\n"
                    + "              Ryan Higgins\n"
                    + "              Michael Schulz\n\n"
                    + "http://heartofgold.sourceforge.net\n"
                    + "Released under the GPL license", "About Heart of Gold",
                    JOptionPane.INFORMATION_MESSAGE);
        }

        if (command.equals("Help")) {
            JOptionPane.showMessageDialog(this,
                    "This function is not yet implemented", "HEEELLLP!",
                    JOptionPane.INFORMATION_MESSAGE);
        }

        if (command.equals("Preferences")) {
            JOptionPane.showMessageDialog(this,
                    "This function is not yet implemented", "Preferences",
                    JOptionPane.INFORMATION_MESSAGE);
        }

        if (command.equals("DON'T PANIC!")) {
            JOptionPane
                    .showMessageDialog(
                            this,
                            "As fans may have guessed, the name Heart of Gold\n"
                                    + " is a tribute to Douglas Adams and his "
                                    + "famous\n"
                                    + " 'Hitchhickers Guide' anthology.\n"
                                    + "\nCurious? Check out "
                                    + "www.douglasadams.com",
                            "RIP Douglas Adams 1952 - 2001",
                            JOptionPane.INFORMATION_MESSAGE, new ImageIcon(
                                    "adams.png"));
        }

        if (command.equals("Main Menu")) {
            l.show(cardPanel, "Main");
        }
    }

    public static boolean switchScreen(final String screen) {
        if (!cards.contains(screen)) {
            return false;
        }
        l.show(cardPanel, screen);
        return true;
    }

    /* Private Data members */
    private String[] labels;
    private JMenuBar menuBar;
    private static CardLayout l;
    private static Vector<Object> cards = new Vector<Object>();
    private static final double version = 1.0;
    private static JPanel cardPanel = new JPanel(new CardLayout());
}
